//
//  SearchCategoryListVC.m
//  CenturyCity
//
//  Created by beisheng03 on 2018/1/15.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "SearchCategoryListVC.h"
#import "GoodCateListLayout.h"
#import "GoodCategoryListCell.h"
#import "OneGoodsModel.h"
#import "GoodDetailController.h"
#import "GoodCategoryHeader.h"
@interface SearchCategoryListVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
/** <#note#> */
@property (nonatomic, weak) UICollectionView *myCollectionView;

@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, strong) NSString *hasmore;

@property (nonatomic, assign) NSString *pageCount;

@property (nonatomic, assign) NSString *totalCount;


@property (nonatomic, strong) NSMutableArray<OneGoodsModel *> *dataModels;

@end

@implementation SearchCategoryListVC

- (UICollectionView *)myCollectionView{
    if (!_myCollectionView) {
        UICollectionView *temp = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:[[GoodCateListLayout alloc]init]];
        
        temp.delegate = self;
        
        temp.dataSource = self;
        
        temp.showsVerticalScrollIndicator = NO;
        
        _myCollectionView = temp;
        
        [self.view addSubview:temp];
    }
    
    
    
    return _myCollectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    
    [self.myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GoodCategoryListCell class]) bundle:nil] forCellWithReuseIdentifier:[GoodCategoryListCell identifierStr]];
    
    
    
    [self.myCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GoodCategoryHeader class]) bundle:nil] forCellWithReuseIdentifier:[GoodCategoryHeader identifierStr]];
    
    MJWeakSelf;
    self.myCollectionView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadData];
        
    }];
    
    [self.myCollectionView.mj_header beginRefreshing];
    
    weakSelf.myCollectionView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        
    }];
    
    //    [self.myCollectionView.mj_footer setAutomaticallyHidden:YES];
    
    [self.myCollectionView.mj_header setAutomaticallyChangeAlpha:YES];
    
    
//    CGFloat navH = IPhoneX ? 88 : 64;
//    CGFloat tabbarH = IPhoneX ? 83 : 49;
//
//    if (@available(iOS 11.0, *)) {
//
//        self.myCollectionView.contentInset = UIEdgeInsetsMake(XMGTitlesViewH, 0, tabbarH, 0);
//
//    }else{
//
//        self.myCollectionView.contentInset = UIEdgeInsetsMake(navH + XMGTitlesViewH, 0, tabbarH,0 );
//
//
//    }
    
    
    // Do any additional setup after loading the view.
}

- (void)loadData{
    
    self.curpage = 1;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"keyword"] = self.keyword;
    parameters[@"page"] = @"1";
    parameters[@"pagesize"] = @"20";
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:GET_METHOD URLPath:@"Good/getGoodsList" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            MYJSONSTR
            
            weakSelf.pageCount = responseObject[@"data"][@"page_count"];
            
            weakSelf.totalCount = responseObject[@"data"][@"total_count"];
            
            
            weakSelf.dataModels = [OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"goods"]];
            
            if (weakSelf.dataModels.count == 1) {
                
                [weakSelf.dataModels addObjectsFromArray:[OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"goods"]]];
                
                weakSelf.dataModels.lastObject.goods_price = nil;
                
            }
            
            
            
            [weakSelf.myCollectionView.mj_footer resetNoMoreData];
            
            if ([weakSelf.totalCount isEqualToString:@"0"]) {
                
                self.logoView.hidden = NO;
                
                //                self.myCollectionView.hidden = YES;
                //                [SVProgressHUD showInfoWithStatus:@"暂无此类商品!"];
                
            } else {
                
                self.logoView.hidden = YES;
                
                //                self.myCollectionView.hidden = NO;
                
                if ([weakSelf.pageCount isEqualToString:@"1"]) {
                    
                    [weakSelf.myCollectionView.mj_footer endRefreshingWithNoMoreData];
                    
                } else {
                    
                    
                    
                }
            }
            
            [weakSelf.myCollectionView.mj_header endRefreshing];
            
            
            
            [weakSelf.myCollectionView reloadData];
            
            
        } else {
            
            [weakSelf.myCollectionView.mj_header endRefreshing];
            
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
    }];
    
    
    
    
    
    
    
}


- (void)loadMoreData{
    
    
    
    self.curpage ++;
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    parameters[@"keyword"] = self.keyword;
    parameters[@"pagesize"] = @"20";
    parameters[@"page"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    [[NetworkTools shareNetworkTools] sendRequestWithHttpMethod:GET_METHOD URLPath:@"Good/getGoodsList?categoryId=" parameters:parameters completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            
            [self.dataModels addObjectsFromArray:[OneGoodsModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"goods"]]];
            
            
            if (self.pageCount.integerValue == self.curpage) {
                
                [self.myCollectionView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [self.myCollectionView.mj_footer endRefreshing];
                
                
            }
            
            
            [self.myCollectionView reloadData];
            
        }else{
            
            [self.myCollectionView.mj_footer endRefreshing];
            
            [SVProgressHUD showInfoWithStatus:error.domain];
            
        }
        
    }];
    
    
}



- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    
    if (self.dataModels == nil) {
        return  0;
    } else {
        return 1;
    }
}





- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.dataModels == nil) {
        return  0;
    } else {
        return self.dataModels.count;
    }
    
}



- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    

        
        GoodCategoryListCell *temp = (GoodCategoryListCell *)[collectionView dequeueReusableCellWithReuseIdentifier:[GoodCategoryListCell identifierStr] forIndexPath:indexPath];
        
        temp.model = self.dataModels[indexPath.item];
        
        
        return temp;
   
    
    
    
    
    
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
  
    OneGoodsModel *model = self.dataModels[indexPath.item];
    
    if (model.goods_price == nil) {
        return;
    }
    
    GoodDetailController *tempVC = [[GoodDetailController alloc]init];
    
    tempVC.goodsModel = model;
    
    
    [self.navigationController pushViewController:tempVC animated:YES];
    
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
  
    return CGSizeMake(KScreenW * 0.5,220);
        
  
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
